package com.class5.springboot.frame.security;

import java.io.Serializable;
/**
 * 全局请求上下文
 * @author Administrator
 */
@SuppressWarnings("serial")
public class RequestContext implements Serializable{
    private static ThreadLocal<RequestContext> cache = new ThreadLocal<RequestContext>();
    //请求ID
    private String id;
    //用户ID
    private Long userId;
    //额外需要的参数
    private String apiData;
    //当前IP
    private String ip;
    //当前token
    private String token;
    //当前是否在调试模式
    private boolean debugModel;

    public RequestContext(String id) {
        this.id = id;
    }
    /**
     * 绑定当前线程的context
     * @param ctx
     */
    public static void bind(RequestContext ctx){
        cache.set(ctx);
    }
    /**
     * 解除当前线程绑定的context
     */
    public static void unbind(){
        try {
            cache.remove();
        } catch (Exception e) {
        }
    }

    public static RequestContext getCurrentContext(){
        return cache.get();
    }

    public String getApiData() {
        return apiData;
    }

    public void setApiData(String apiData) {
        this.apiData = apiData;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public String getToken() {
        return token;
    }
    public void setToken(String token) {
        this.token = token;
    }
    public boolean isDebugModel() {
        return debugModel;
    }
    public void setDebugModel(boolean debugModel) {
        this.debugModel = debugModel;
    }

}
